package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dao.RbacRoleDao;
import com.woniuxy.entity.RbacManager;
import com.woniuxy.entity.RbacRole;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.RbacRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;


@Service
public class RbacRoleServiceImpl implements RbacRoleService {
    @Resource //和Autowired明显区别是，优先通过byName去容器中找有没有这个bean，没有的话再用byType去找。
    @Qualifier("rbacRoleDao")//如果是bean的名字和这里的属性名字对应不上，则可以手动指定寻找那个Bean。
    RbacRoleDao rbacRoleDao;
    @Override
    public ResponseEntity findAll(int currentPage,int pageSize) {
        PageHelper.startPage(currentPage, pageSize);//只要在这里传入了分页信息
        List<RbacRole> all = rbacRoleDao.findAll();
        PageInfo pageInfo = new PageInfo(all);//就能够封装分页信息
        return new ResponseEntity(200,"请求成功！",pageInfo);
    }

    @Override
    public ResponseEntity findOne(int id) {
        RbacRole rbacRole = rbacRoleDao.findOne(id);
        return new ResponseEntity(200,"查询成功",rbacRole);
    }

    @Override
    public ResponseEntity addOne(RbacRole rbacRole) {
        int i = rbacRoleDao.addOne(rbacRole);
        if (i>0) {
            return new ResponseEntity(200, "新增成功", null);
        }else {
            return new ResponseEntity(500, "新增失败", null);
        }
    }

    @Override
    public ResponseEntity deleteOne(int id) {
        int i = rbacRoleDao.deleteOne(id);
        if (i>0) {
            return new ResponseEntity(200, "删除成功", null);
        }else {
            return new ResponseEntity(500, "删除失败", null);
        }
    }

    @Override
    public ResponseEntity updateOne(RbacRole rbacRole) {
        int i = rbacRoleDao.updateOne(rbacRole);
        if (i>0) {
            return new ResponseEntity(200, "编辑成功", null);
        }else {
            return new ResponseEntity(500, "编辑失败", null);
        }
    }

    @Override
    public Set<String> findOneByUserName(String username) {

        return rbacRoleDao.findOneByUserName(username);
    }

    @Override
    public ResponseEntity editType(int id) {
        int i = rbacRoleDao.editType(id);
        if (i>0){
            return new ResponseEntity(200,"成功更新"+id+"这条数据状态",null);
        }else {
            return new ResponseEntity(500,"更新失败",null);
        }
    }

    @Override
    public ResponseEntity search(int currentPage, int pageSize, String name, String code) {
        PageHelper.startPage(currentPage,pageSize);
        List<RbacRole> list= rbacRoleDao.search(name,code);
        PageInfo pageInfo = new PageInfo(list);
        return new ResponseEntity(200, "", pageInfo);
    }
}
